//package com.zt.csp.shorturl.interceptor;
//
//
//import com.zt.csp.shorturl.dto.ServiceResult;
//import com.zt.csp.shorturl.utils.ThreadLocalUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * 用户登录拦截器
// * Created by zt on 2018/4/25 0025
// */
//@Component
//public class UserInterceptor extends HandlerInterceptorAdapter {
//    private static final Logger logger = LoggerFactory.getLogger(UserInterceptor.class);
//
//    @Autowired
//    private CSPKernelSDK sdk;
//
//    @Autowired
//    private SDKKernelConfig config;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
//        String authorization = request.getHeader("Authorization");
//        if (authorization.startsWith("Bearer ")) {
//            String substring = authorization.substring(6);
//            try {
//                Resp<TokenDeliverDTO> tokenDeliverDTOResp = sdk.user.tenantValidate(
//                        substring,
//                        "",
//                        config
//                );
//                TokenDeliverDTO body = tokenDeliverDTOResp.getBody();
//                String appId = body.getAppId();
//                String userId = body.getUserId();
//                logger.info("addId = {},userId = {}", appId, userId);
//                ThreadLocalUtils.USER_THREAD_LOCAL.set(userId);
//                return true;
//            } catch (Exception e) {
//                logger.info("检测授权失败", e);
//                String jsonString = ServiceResult.failed(1002, e.getMessage()).toString();
//                response.setCharacterEncoding("UTF-8");
//                response.setContentType("application/json; charset=utf-8");
//                response.getWriter().print(jsonString);
//            }
//        } else {
//            logger.info("没有找到用户信息");
//            String jsonString = ServiceResult.NO_LOGIN.toString();
//            response.setCharacterEncoding("UTF-8");
//            response.setContentType("application/json; charset=utf-8");
//            response.getWriter().print(jsonString);
//        }
//        return false;
//    }
//
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        ThreadLocalUtils.USER_THREAD_LOCAL.remove();
//        super.afterCompletion(request, response, handler, ex);
//    }
//}
